package com.acme.domain.bank;

import com.acme.domain.account.Account;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Client implements Serializable {
    private final String name;
    private final Gender gender;

    private final Set<Account> accounts = new HashSet<Account>();

    public Client(final String name, final Gender gender) {
        this.name = name;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public String getSalutation() {
        if (gender != null) {
            return gender.getSalutation() + " " + name;
        } else {
            return name;
        }
    }

    public void addAccount(final Account account) {
        accounts.add(account);
    }

    public Set<Account> getAccounts() {
        return Collections.unmodifiableSet(accounts);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder(getSalutation()).append(" accounts:\n");
        for (Account account : accounts) {
            builder.append("-").append(account);
        }
        return builder.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Client client = (Client) o;

        if (accounts != null ? !accounts.equals(client.accounts) : client.accounts != null) return false;
        if (gender != client.gender) return false;
        if (name != null ? !name.equals(client.name) : client.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        result = 31 * result + (accounts != null ? accounts.hashCode() : 0);
        return result;
    }
}